package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.StringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.*;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowEntrustRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Huhangfei
 * @date 2021-11-17 09:18
 */
@RestController
@RequestMapping(value = "/entrust")
@Api(value = "流程委托", tags = {"流程委托"})
@Slf4j
public class FlowEntrustController {
    @Autowired
    private FlowEntrustRepository flowEntrustRepository;
    @ApiOperation("新增或者修改当前用户的默认委派数据和自定义委派数据")
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    public ResponseResult insertOrUpdateEntrust(@RequestBody FlowEntrustVo flowEntrustVo){
        return flowEntrustRepository.insertOrUpdate(flowEntrustVo);
    }

    @ApiOperation("选择当前用户的默认委派数据和自定义委派数据")
    @RequestMapping(value = "/selectByClient", method = RequestMethod.POST)
    public ResponseResult selectEntrustAndDetailById(@RequestBody FlowEntrustQueryVo flowEntrustQueryVo){
        return ResponseResult.success(flowEntrustRepository.selectEntrustById(flowEntrustQueryVo));
    }


    @ApiOperation("删除默认委派数据（同时删除自定义委派数据）")
    @RequestMapping(value = "/deleteData", method = RequestMethod.POST)  //删除委派数据
    public ResponseResult deleteData(@RequestBody FlowEntrustVo flowEntrustVo){
        return flowEntrustRepository.deleteFlowEntrustData(flowEntrustVo);
    }

    @ApiOperation("删除自定义委派数据")
    @RequestMapping(value = "/deleteDetailData", method = RequestMethod.POST)  //删除自定义委派数据
    public ResponseResult deleteDetailData(@RequestBody String ids){
        JSONObject params= JSON.parseObject(ids);
        JSONArray array=params.getJSONArray("ids");
        List<Integer> list=new ArrayList<>();
        for(int i=0;i<array.size();i++){
            list.add(array.getInteger(i));
        }
        if(flowEntrustRepository.deleteFlowEntrustDetailData(list)){
            return ResponseResult.success("删除自定义委托成功");
        }
        return ResponseResult.fail(100,"删除失败，请联系运维人员");
    }

    @ApiOperation("修改默认委托和自定义委托的状态")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public ResponseResult updateStatus(@RequestBody FlowEntrustUpdateVo flowEntrustUpdateVo){
       return flowEntrustRepository.updateStatus(flowEntrustUpdateVo);
    }

    @ApiOperation("删除默认委托和自定义委托")
    @RequestMapping(value = "/deleteEntrustOrDetail", method = RequestMethod.POST)
    public ResponseResult deleteEntrustOrDetail(@RequestBody FlowEntrustUpdateVo flowEntrustUpdateVo){
        return flowEntrustRepository.deleteEntrustOrDetail(flowEntrustUpdateVo);
    }

    @ApiOperation("根据委托人和流程名称查询委托数据")
    @RequestMapping(value = "/selectByClientAndModelKey", method = RequestMethod.POST)
    public ResponseResult selectByClientAndModelKey(@RequestBody FlowEntrustQueryForComputeVo flowEntrustQueryForComputeVo){
        return ResponseResult.success(flowEntrustRepository.selectByClientAndModelKey(flowEntrustQueryForComputeVo));
    }

//    @ApiOperation("条件查询委托数据")
//    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
//    public ResponseResult selectByCondition(@RequestBody FlowEntrustQueryVo flowEntrustQueryVo){
//        PageInfo<FlowEntrustVo> pageInfos = new PageInfo<>(flowEntrustRepository.selectEntrustByCondition(flowEntrustQueryVo));
//        return ResponseResult.success(pageInfos.getList()).setTotalCount((int) pageInfos.getTotal())
//                .setPageNum(pageInfos.getPageNum()).setPageSize(pageInfos.getPageSize());
//    }
    @ApiOperation("根据组织编码获取组织名称")
    @RequestMapping(value = "/getOrgName", method = RequestMethod.POST)
    public ResponseResult getOrgNameByCode(@RequestBody String orgCode){
        JSONObject params= JSON.parseObject(orgCode);
        String orgCode1=params.getString("orgCode");
        if(StringUtils.isBlank(orgCode1)){
            return ResponseResult.fail(100,"输入的参数不能为空");
        }
        return ResponseResult.success(flowEntrustRepository.transferOrgCodeToName(orgCode1));
    }

    @ApiOperation("条件查询测试")
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public ResponseResult selectByCondition(@RequestBody FlowEntrustQueryVo flowEntrustQueryVo){
        PageInfo<FlowEntrustVo> pageInfos = new PageInfo<>(flowEntrustRepository.selectEntrustByCondition2(flowEntrustQueryVo));
        return ResponseResult.success(pageInfos.getList()).setTotalCount((int) pageInfos.getTotal())
                .setPageNum(pageInfos.getPageNum()).setPageSize(pageInfos.getPageSize());
    }
}
